// omp pi parallel
// 2020.10.15
// whsu
// @see notes/cpp_objects.md

#include <stdio.h>
#include <stdlib.h>
#include <omp.h>

FILE *randomFile;

u_int32_t randomNum() {
    u_int32_t ret;
    fread((char*)(&ret), sizeof(u_int32_t), 1, randomFile);
    return ret;
}

int main() {
    
    // ! Don't ever use `std::fstream` in cpp, you'll regret it
    randomFile = fopen("/dev/urandom", "rb");
    int64_t inCircleHits = 0;
    int64_t totalHits = 100000000;

#pragma omp parallel for num_threads(12) reduction(+:inCircleHits)
    for (int i=0 ; i<totalHits ; i++) {
        double x=1.0*randomNum()/__UINT32_MAX__;
        double y=1.0*randomNum()/__UINT32_MAX__;
        if ((x*x+y*y)<=1.0) {
            inCircleHits++;
        }   
    }   
    
    printf("%f", 4.0*inCircleHits/totalHits);
    fclose(randomFile);
    fflush(stdout);
    return 0;
    
}

